package com.aoxin.controller.opm;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.aoxin.model.dto.SelectDto;
import com.aoxin.model.opm.SlfEmployee;
import com.aoxin.service.inter.opm.EmployeeService;
import com.aoxin.util.AjaxRes;
import com.aoxin.util.Const;
import com.aoxin.util.ThreadLocalUtil;
import com.aoxin.util.enumerate.OperateType;
import com.aoxin.util.log.LogInfo;

/**
 * 员工Controller
 */
@Controller
@ResponseBody
@RequestMapping("/web/employee")
public class EmployeeController {

	@Autowired
	private EmployeeService employeeService;

	/**
	 * 添加员工
	 * 
	 * @param employee
	 *            员工
	 */
	@RequestMapping("/insertEmployee")
	@LogInfo(content = "员工", operateType = OperateType.INSERT)
	public AjaxRes insertEmployee(SlfEmployee employee, AjaxRes ajaxRes,Integer roleId) {
		try {
			Integer employeeId = this.employeeService.insertEmployee(employee,roleId);
			ajaxRes.setSucceed(employeeId, Const.SAVE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量删除员工
	 * 
	 * @param employeeIds
	 *            员工ID集合
	 */
	@RequestMapping("/deleteEmployeeByIds")
	@LogInfo(content = "员工", operateType = OperateType.DELETE)
	public AjaxRes deleteEmployeeByIds(@RequestParam("employeeIds") List<Integer> employeeIds, AjaxRes ajaxRes) {
		try {
			this.employeeService.deleteEmployeeByIds(employeeIds);
			ajaxRes.setSucceedMsg(Const.DEL_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 修改员工信息
	 * 
	 * @param employee
	 *            员工信息
	 */
	@RequestMapping("/updateEmployee")
	@LogInfo(content = "员工", operateType = OperateType.UPDATE)
	public AjaxRes updateEmployee(SlfEmployee employee, AjaxRes ajaxRes) {
		try {
			this.employeeService.updateEmployee(employee);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 批量修改员工状态
	 * 
	 * @param employeeIds
	 *            员工ID集合
	 * @param state
	 *            状态
	 */
	@RequestMapping("/updateStates")
	@LogInfo(content = "员工", operateType = OperateType.UPDATE)
	public AjaxRes updateStates(@RequestParam("employeeIds") List<Integer> employeeIds, @RequestParam("state") Integer state, AjaxRes ajaxRes) {
		try {
			this.employeeService.updateStates(employeeIds, state);
			ajaxRes.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询员工
	 * 
	 * @param employeeId
	 *            员工ID
	 * @return 员工
	 */
	@RequestMapping("/getEmployee")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getEmployee(@RequestParam("employeeId") Integer employeeId, AjaxRes ajaxRes) {
		try {
			SlfEmployee employee = this.employeeService.getEmployee(employeeId);
			ajaxRes.setSucceed(employee, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 分页查询员工
	 * 
	 * @param map
	 *            条件集合
	 * @return 员工集合
	 */
	@RequestMapping("/getEmployeeList")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getEmployeeList(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfEmployee> employees = this.employeeService.getEmployeeList(map);
			ajaxRes.setSucceed(employees, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 获取当前操作员
	 * 
	 * @return 操作员信息
	 */
	@RequestMapping("/getOperator")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getOperator(HttpServletRequest request, AjaxRes ajaxRes) {
		try {
			SlfEmployee operator = ThreadLocalUtil.getEmployee();
			ajaxRes.setSucceed(operator, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 查询员工
	 * 
	 * @param map
	 *            条件集合
	 * @return 员工集合
	 */
	@RequestMapping("/getEmployees")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getEmployees(@RequestParam Map<String, Object> map, AjaxRes ajaxRes) {
		try {
			List<SlfEmployee> employees = this.employeeService.getEmployeeList(map);
			ajaxRes.setSucceed(employees, Const.DATA_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}

	/**
	 * 初始员工密码
	 * 
	 * @param employeeIds
	 * @param oldPassword
	 * @param newPassword
	 * @param type
	 * @param res
	 * @return
	 */
	@RequestMapping("/iniPassword")
	@LogInfo(content = "员工", operateType = OperateType.UPDATE)
	public AjaxRes iniPassword(@RequestParam("employeeIds") List<Integer> employeeIds, String oldPassword, String newPassword, Integer type, AjaxRes res) {
		try {
			this.employeeService.iniPassword(employeeIds, oldPassword, newPassword, type);
			res.setSucceedMsg(Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			res.setFailMsg(e.getMessage());
		}
		return res;
	}

	/**
	 * 查询下拉人员
	 */
	@RequestMapping("/getEmployeeListByOrg")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getEmployeeListByOrg(AjaxRes ajaxRes) {
		try {
			List<SelectDto> selectDtos = this.employeeService.getEmployeeListByOrg();
			ajaxRes.setSucceed(selectDtos, Const.UPDATE_SUCCEED);
		} catch (Exception e) {
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
	/**
	 * 根据学校Id查询学校下的教师
	 * @param schoolId
	 * @return
	 */
	@RequestMapping("/getTeacherBySchoolId")
	@LogInfo(content = "员工", operateType = OperateType.QUERY)
	public AjaxRes getTeacherBySchoolId(AjaxRes ajaxRes,Integer schoolId){
		try {
			List<SelectDto> selectDtoList = this.employeeService.getTeacherBySchoolId(schoolId);
			ajaxRes.setSucceed(selectDtoList,Const.DATA_SUCCEED);
		}catch (Exception e){
			ajaxRes.setFailMsg(e.getMessage());
		}
		return ajaxRes;
	}
}
